<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct()
	 {
		parent::__construct();
		$this->load->model('Function_m','fun');
		$this->load->model('Exceptionerror_m','m');
		$this->load->model('Report_m','R');
		$this->pageSize =$this->config->item('pagenum');
	 }
	public function index1()
	{

		if($_COOKIE['userid']==295 || $_COOKIE['userid']==1|| $_COOKIE['userid']==287 || $_COOKIE['userid']==302){
			$this->index1();
		}else{
			$this -> tpl -> display("welcome1.tpl");
		}
	}
		
	public function index()
	{
	
		if($_COOKIE["ext_userid"] && $_COOKIE["ext_userid_open_goods"] ==0)	//外部
		{
			$this -> tpl -> display("welcome1.tpl");
			die;
		}
		/***********系统公告开始**********/
		$sysAnnouncement = $this->config->item('sysAnnouncement');
		if($sysAnnouncement)
		{
			$this->tpl->assign("sysAnnouncement", $sysAnnouncement);
		}
		/***********系统公告结束**********/
		/*******
		  当前没有填写成本价的商品	 
		 *****/
		if($_COOKIE["department"] ==1 || $_COOKIE["department"] ==302)
		{
		 $nocostprice = $this->m->getnocostprice();
		 $this->tpl->assign("nocostprice", $nocostprice);
		}
		
		if($_COOKIE["userid"] ==295)
		{
		 $gettwogoods = $this->m->gettwogoods();
		 if($_SERVER['HTTP_HOST'] =='localhost:82'){
		 }else{
			 $this->load->library('AmazondataPro');
			 $ress = $this->amazondatapro->checkAmazonOrder();
			 if($ress)
			 {
				$checkAmazonOrder = "亚马逊订单出现问题请核查：".implode(",",$ress);
			 }
		 }
		 $this->tpl->assign("gettwogoods", $gettwogoods);
		$this->tpl->assign("checkAmazonOrder", $checkAmazonOrder);
		}
		
		

		/***********停产商品开始**********/
		$workforu = $this->m->sysworkbench(4);
		$this->tpl->assign("workforulist", $workforu['list']);
		$this->tpl->assign("workforutotal", $workforu['total']);
		/***********停产商品结束**********/
		
		/***********下架商品开始**********/
		$listxia = $this->m->sysworkbench(6);
		$this->tpl->assign("listxia", $listxia['list']);
		$this->tpl->assign("listxiatotal", $listxia['total']);
		/***********下架商品结束**********/


		/***********清仓商品开始**********/
		$listqing = $this->m->sysworkbench(5);
		$this->tpl->assign("listqing", $listqing['list']);
		$this->tpl->assign("listqingtotal", $listqing['total']);
		/***********清仓商品结束**********/

		/***********重新销售商品**********/
		$listchong = $this->m->sysworkbench(2);
		$this->tpl->assign("listchong", $listchong['list']);
		$this->tpl->assign("listchongtotal", $listchong['total']);
		/***********重新销售结束**********/

		/************最新新品**********/
		$newproduct = $this->m->newproduct();
		$this->tpl->assign("newproduct", $newproduct);
		/***********最新新品结束**********/

		/************最新推荐新品**********/
		$newrecommend = $this->m->newrecommend();
		$this->tpl->assign("newrecommend", $newrecommend);
		/***********最新推荐结束**********/

		/************工厂扫件**********/
		$newfactoryscan = $this->m->newfactoryscan();
		$this->tpl->assign("newfactoryscan", $newfactoryscan);
		/***********工厂扫件结束**********/

		/************店铺未下单列表**********/
		$shopnoorder = $this->m->shopnoorder();
		$this->tpl->assign("shopnoorder",$shopnoorder);
		$this->tpl->assign("shopdays",$this->config->item("shopdays"));
		/***********店铺未下单结束**********/

		/************店铺fba未下单列表**********/
		$shopnofbaorder = $this->m->shopnofbaorder();
		$this->tpl->assign("shopnofbaorder", $shopnofbaorder);
		$this->tpl->assign("fbashopdays",$this->config->item("fbashopdays"));
		/***********店铺fba未下单结束**********/

		
		/************最近入库数**********/
		$newstock = $this->m->newstock();
		$this->tpl->assign("newstock", $newstock);
		/***********最近入库数**********/
		$shopname = $this->m->getshop();
		if($_COOKIE["department"] ==1 || $shopname)
		{
			$this->tpl->assign("showchart", 1);
		}else{
			$this->tpl->assign("showchart", 0);
		}
		
		$this->tpl->assign("userid", $_COOKIE["userid"]);

		/************最新消息**********/
		$newmessage = $this->m->newmessage();
		$this->tpl->assign("listmessage", $newmessage['list']);
		$this->tpl->assign("listmessagetotal", $newmessage['total']);
		
		if($_COOKIE["classid"] == 16||$_COOKIE["classid"] == 13||$_COOKIE["classid"] == 12)	//外部
		{
			$this -> tpl -> display("welcome1.tpl");
			die;
		}

		/*
		if($_COOKIE['userid']==295 || $_COOKIE['userid']==287 || $_COOKIE['userid']==302){
			$this -> tpl -> display("welcome.tpl");
		}else{
			$this -> tpl -> display("welcome1.tpl");
		}*/
		$this -> tpl -> display("welcome.tpl");

	}

	function ajaxbutton()
	{
		$op = $this->input->get_post('op');
		if($op == 'getnexthasdstock')
		{
			//获取下10条数据
			$num = $this->input->post('num');
			$newstock = $this->m->newstock($num);
			echo json_encode($newstock);
			die;
		}
		if($op == 'getnextrecommend')
		{
			//获取下10条数据
			$num = $this->input->post('num');
			$newstock = $this->m->newrecommend($num);
			echo json_encode($newstock);
			die;
		}
		if($op =='getchart')
		{
			/****************订单统计信息*************/
		$starttime =date("Y-m-01");		//开始时间
    	$endtime   = date("Y-m-d");		//结束时间
		$ntday = $this->m->diffBetweenTwoDays($starttime,$endtime);
		/*****************数据轴天数***********************************/
			$tickstext="";
			$ticks="[";
			for($i=1;$i<=$ntday;$i++)
			{
				$tickstext.= ' ['.$i.', "'.$i.'号"],';
			}
			$tickstext = trim($tickstext,",");
			$ticks.=$tickstext;
			$ticks.="]";
			$this->tpl->assign("ticks", $ticks);
		/*****************数据轴天数结束***********************************/
	
		$orderlist = $this->m->getordercount($starttime,$endtime);
		/*****************图表数据1***********************************/
		$blist = array_reverse($orderlist['list']);//重新排序，最小的天数在前面
		$valuetext="";
		$ts="";
		$vlues="[";
		foreach($blist as $key=>$rs)
		{
			$keys = $key+1;
			$valuetext.= ' ['.$keys.', "'.$rs['subtotal'].'"],';
			$ts[]=$rs['subtotal'];
		}
		$valuetext = trim($valuetext,",");
		$vlues.=$valuetext;
		$vlues.="]";
		$this->tpl->assign("vlues", $vlues);
		/*****************图表数据1结束***********************************/
		$this->tpl->assign("order", $orderlist);
		
		
		//读取去年马帮的同时间统计信息
		$this->load->library('Mabang');
		$tstarttime = date('Y-m-01', strtotime('-1 year'));
		$tendtime = date('Y-m-d', strtotime('-1 year'));

		$oldorderlist = $this->mabang->getordercount($tstarttime,$tendtime);//读取去年马帮的同时间统计信息
		$this->tpl->assign("oldorder", $oldorderlist);
		/*****************图表数据2***********************************/
			$blist = array_reverse($oldorderlist['list']);//重新排序，最小的天数在前面
			$valuetext="";
			$vlues="[";
			foreach($blist as $key=>$rs)
			{
				$keys = $key+1;
				$valuetext.= ' ['.$keys.', "'.$rs['subtotal'].'"],';
				$ts[]=$rs['subtotal'];
			}
			$valuetext = trim($valuetext,",");
			$vlues.=$valuetext;
			$vlues.="]";
			$this->tpl->assign("vlues2", $vlues);
		/*****************图表数据2结束***********************************/
	
		/****************订单统计结束*************/
		$pos = array_search(max($ts), $ts);
		$this->tpl->assign("maxtop", $ts[$pos]);//最大值

		$this->tpl->assign("lasttime",date("Y-m-d",strtotime("-1 day")));
		$this -> tpl -> display("welcome_chart.tpl",0,0,1);
		}
		//确认上架  最新推荐商品
		if($op=="sureAddGoods"){
			$id = $this->input->post('id');
			$flag = $this->input->post('flag');
			$address = $this->input->post('address');
			$types = $this->input->post('types');
			if($types==1){
					$userid = $_COOKIE['userid'];
					$sql = "SELECT * FROM db_external_buy WHERE id = '{$id}' ";
					$rs  = $this->db->query($sql)->row_array();
					$reason = $userid."-".$flag."-".$address;
					if($rs){
						//返回具体原因
						if($rs['return_reason']){
							$ret_reason = $rs['return_reason'].",".$reason;
						}else{
							$ret_reason = $reason;
						}
						if($rs['return_receivers']){
							$ret_receivers = $rs['return_receivers'].",".$userid;
						}else{
							$ret_receivers = $userid;
						}
						$this->db->update("db_external_buy",array('return_receivers'=>$ret_receivers,'return_reason'=>$ret_reason)," id = '".$rs['id']."'");

						if($this->db->affected_rows()){
							echo 200;exit;
						}else{
							echo 201;exit;
						}


					}else{
						echo 201;exit;
					}
			}elseif($types==2){
				$userid = $_COOKIE['userid'];
				$sql = "SELECT * FROM db_product_develop WHERE id = '{$id}' ";
				$rs  = $this->db->query($sql)->row_array();
				$reason = $userid."-".$flag."-".$address;
				if($rs){
					//返回具体原因
					if($rs['return_reason']){
						$ret_reason = $rs['return_reason'].",".$reason;
					}else{
						$ret_reason = $reason;
					}
					if($rs['return_receivers']){
						$ret_receivers = $rs['return_receivers'].",".$userid;
					}else{
						$ret_receivers = $userid;
					}
					$this->db->update("db_product_develop",array('return_receivers'=>$ret_receivers,'return_reason'=>$ret_reason)," id = '".$rs['id']."'");

					//记录上架时间 或 取消上架时间
					$arr_shelf = array();
					$arr_shelf['develop_id'] = $id;
					$arr_shelf['sku'] = $rs['sku'];
					$arr_shelf['type'] = $flag;
					$arr_shelf['linkUrl'] = $address;
					$arr_shelf['createUid'] = $_COOKIE['userid'];
					$arr_shelf['createUser'] = $_COOKIE['username'];
					$arr_shelf['addTime'] = date("Y-m-d H:i:s");
					$this->db->insert("db_product_develop_addtime",$arr_shelf); //插入数据不更新


					if($this->db->affected_rows()){
						echo 200;exit;
					}else{
						echo 201;exit;
					}

				}else{
					echo 201;exit;
				}

			}else{
					$userid = $_COOKIE['userid'];
					$sql = "SELECT * FROM db_goods_recommend WHERE id = '{$id}' ";
					$rs  = $this->db->query($sql)->row_array();
					$reason = $userid."-".$flag."-".$address;
					if($rs){
						//返回具体原因
						if($rs['return_reason']){
							$ret_reason = $rs['return_reason'].",".$reason;
						}else{
							$ret_reason = $reason;
						}
						if($rs['return_receivers']){
							$ret_receivers = $rs['return_receivers'].",".$userid;
						}else{
							$ret_receivers = $userid;
						}
						$this->db->update("db_goods_recommend",array('return_receivers'=>$ret_receivers,'return_reason'=>$ret_reason)," id = '".$rs['id']."'");

						if($this->db->affected_rows()){
							echo 200;exit;
						}else{
							echo 201;exit;
						}

					}else{
						echo 201;exit;
					}
			
			}

		}
		//显示更多推荐信息
		if($op=="showMoreRecommend"){
			//外采购
			$nowpage = $_REQUEST["pageid"];
			if ($nowpage == "")
				$nowpage = 1;
			$userid = $_COOKIE['userid'];
			$where = "";
			$sku = trim($this->input->get('other'));
			if($sku){
				$where.= " AND sku = '{$sku}' ";
				$this->tpl->assign("other",$sku);
			}
			$status = trim($this->input->get('status'));
			if($status){
				if($status==1)
				{
					//$where.=" AND return_receivers like '%,".$userid.",%'";
					$where.=" AND return_receivers REGEXP({$userid})";
				}else{
					//$where.=" AND (return_receivers IS null OR return_receivers NOT like '%,".$userid.",%')";
					$where.=" AND (return_receivers IS null OR return_receivers NOT REGEXP ({$userid}))";
				}
				$this->tpl->assign("status",$status);
			}
			$type = trim($this->input->get("type"));
			if($type){
				$this->tpl->assign("type",$type);
				if($type==1){
					$wheres= " AND del_flag = 0 AND receivers like '%,".$userid.",%' ";
					$orderby = " ORDER BY return_reason asc ";
					$list = $this->fun->getalllist('db_external_buy', $wheres.$where, $nowpage, $if_csv, $this->pageSize, $orderby);
					$total = $list["total"];
					$totalpage = ceil($list["total"] / $this->pageSize);

					foreach ($list["list"] as $key => $value) {
						$return_receiver = explode(",",$value['return_receivers']);
						$return_reason   = explode(",",$value['return_reason']);
						if(in_array($_COOKIE['userid'],$return_receiver)){
							$list["list"][$key]['cancel_flag'] = 0;
							$list["list"][$key]['cancel_reason'] = 0;
							foreach ($return_reason as $kk => $vv) {
								$arr_reason = explode("-", $vv);
								if ($_COOKIE['userid'] == $arr_reason[0]) {
									$list["list"][$key]['cancel_flag'] = $arr_reason[1];
									$str_con = $arr_reason[0] . "-" . $arr_reason[1] . "-";
									$list["list"][$key]['cancel_reason']  = str_replace($str_con, "", $vv);
									break;
								}
							}
							$list["list"][$key]["return_flag"] = 1;
						}else{
							$list["list"][$key]["return_flag"] = 0;
						}
						$list["list"][$key]["external_buy"] = 1;
					}
				}
				if($type==2){
					//在售产品
					if(count($list["list"]))
						$nkey = count($list["list"])+1;
					else
						$nkey=0;
					$wheres= " AND is_stop = 0 AND receivers like '%,".$userid.",%' ";
				
					
					$orderby = " ORDER BY return_reason asc ";
					$dates = $this->fun->getalllist('db_goods_recommend', $wheres.$where, $nowpage, $if_csv, $this->pageSize, $orderby);

					$total = $dates["total"]+$total;
					$t_page = ceil($dates["total"] / $this->pageSize);
					if($t_page >$totalpage){
						$totalpage = $t_page;
					}

					foreach ($dates["list"] as $key => $value) {

						$list["list"][$nkey]["p_parent_name"] = $value['goods_name'];
						$list["list"][$nkey]["sku"] = $value['parent_sku'];
						$list["list"][$nkey]["id"] = $value['id'];
						$list["list"][$nkey]["protector"] = $value['recommend'];
						$list["list"][$nkey]["linkUrl"] = $value['linkUrl'];


						$return_receiver = explode(",",$value['return_receivers']);
						$return_reason   = explode(",",$value['return_reason']);
						if(in_array($_COOKIE['userid'],$return_receiver)){
							$list["list"][$nkey]['cancel_flag'] = 0;
							$list["list"][$nkey]['cancel_reason'] = 0;
							foreach ($return_reason as $kk => $vv) {
								$arr_reason = explode("-", $vv);
								if ($_COOKIE['userid'] == $arr_reason[0]) {
									$list["list"][$nkey]['cancel_flag'] = $arr_reason[1];
									$str_con = $arr_reason[0] . "-" . $arr_reason[1] . "-";
									$list["list"][$nkey]['cancel_reason']  = str_replace($str_con, "", $vv);
									break;
								}
							}
							$list["list"][$nkey]["return_flag"] = 1;
						}else{
							$list["list"][$nkey]["return_flag"] = 0;
						}
						$list["list"][$nkey]["external_buy"] = 0;
						$nkey = $nkey+1;
					}
				}
				if($type==3){
					//新品开发

					if(count($list["list"]))
						$newkey = count($list["list"])+1;
					else
						$newkey=0;
					$wheres= " AND receivers like '%,".$userid.",%' ";
					
					
					$orderby = " ORDER BY return_reason asc ";
					$dates = $this->fun->getalllist('db_product_develop', $wheres.$where, $nowpage, $if_csv, $this->pageSize, $orderby);
					$total = $dates["total"]+$total;
					$t_page = ceil($dates["total"] / $this->pageSize);
					if($t_page >$totalpage){
						$totalpage = $t_page;
					}

					foreach ($dates["list"] as $key => $value) {

						$list["list"][$newkey]["p_parent_name"] = $value['name'];
						$list["list"][$newkey]["sku"] = $value['sku'];
						$list["list"][$newkey]["id"] = $value['id'];
						$list["list"][$newkey]["protector"] = $value['createName'];
						$list["list"][$newkey]["linkUrl"] = $value['linkUrl'];

						$return_receiver = explode(",",$value['return_receivers']);
						$return_reason   = explode(",",$value['return_reason']);
						if(in_array($_COOKIE['userid'],$return_receiver)){
							$list["list"][$newkey]['cancel_flag'] = 0;
							$list["list"][$newkey]['cancel_reason'] = 0;
							foreach ($return_reason as $kk => $vv) {
								$arr_reason = explode("-", $vv);
								if ($_COOKIE['userid'] == $arr_reason[0]) {
									$list["list"][$newkey]['cancel_flag'] = $arr_reason[1];
									$str_con = $arr_reason[0] . "-" . $arr_reason[1] . "-";
									$list["list"][$newkey]['cancel_reason']  = str_replace($str_con, "", $vv);
									break;
								}
							}
							$list["list"][$newkey]["return_flag"] = 1;
						}else{
							$list["list"][$newkey]["return_flag"] = 0;
						}
						$list["list"][$newkey]["external_buy"] = 2;
						$newkey = $newkey+1;
					}

				}


			}else{
				$wheres= " AND del_flag = 0 AND receivers like '%,".$userid.",%' ";
				$orderby = " ORDER BY return_reason asc ";
				$list = $this->fun->getalllist('db_external_buy', $wheres.$where, $nowpage, $if_csv, $this->pageSize, $orderby);
				$total = $list["total"];
				$totalpage = ceil($list["total"] / $this->pageSize);

				foreach ($list["list"] as $key => $value) {
					$return_receiver = explode(",",$value['return_receivers']);
					$return_reason   = explode(",",$value['return_reason']);
					if(in_array($_COOKIE['userid'],$return_receiver)){
						$list["list"][$key]['cancel_flag'] = 0;
						$list["list"][$key]['cancel_reason'] = 0;
						foreach ($return_reason as $kk => $vv) {
							$arr_reason = explode("-", $vv);
							if ($_COOKIE['userid'] == $arr_reason[0]) {
								$list["list"][$key]['cancel_flag'] = $arr_reason[1];
								$str_con = $arr_reason[0] . "-" . $arr_reason[1] . "-";
								$list["list"][$key]['cancel_reason']  = str_replace($str_con, "", $vv);
								break;
							}
						}
						$list["list"][$key]["return_flag"] = 1;
					}else{
						$list["list"][$key]["return_flag"] = 0;
					}
					$list["list"][$key]["external_buy"] = 1;
				}

				//在售产品
				if(count($list["list"]))
					$nkey = count($list["list"])+1;
				else
					$nkey=0;
				$where= " AND is_stop = 0 AND receivers like '%,".$userid.",%' ";
				$sku = trim($this->input->get('other'));
				if($sku){
					$where.= " AND parent_sku = '{$sku}' ";
					$this->tpl->assign("other",$sku);
				}
				$status = trim($this->input->get('status'));
				if($status){
					if($status==1)
					{
						$where.=" AND return_receivers REGEXP({$userid})";
					}else{
						$where.=" AND (return_receivers IS null OR return_receivers NOT REGEXP ({$userid}))";
					}
				}
				$orderby = " ORDER BY return_reason asc ";
				$dates = $this->fun->getalllist('db_goods_recommend', $where, $nowpage, $if_csv, $this->pageSize, $orderby);

				$total = $dates["total"]+$total;
				$t_page = ceil($dates["total"] / $this->pageSize);
				if($t_page >$totalpage){
					$totalpage = $t_page;
				}

				foreach ($dates["list"] as $key => $value) {

					$list["list"][$nkey]["p_parent_name"] = $value['goods_name'];
					$list["list"][$nkey]["sku"] = $value['parent_sku'];
					$list["list"][$nkey]["id"] = $value['id'];
					$list["list"][$nkey]["protector"] = $value['recommend'];
					$list["list"][$nkey]["linkUrl"] = $value["linkUrl"];


					$return_receiver = explode(",",$value['return_receivers']);
					$return_reason   = explode(",",$value['return_reason']);
					if(in_array($_COOKIE['userid'],$return_receiver)){
						$list["list"][$nkey]['cancel_flag'] = 0;
						$list["list"][$nkey]['cancel_reason'] = 0;
						foreach ($return_reason as $kk => $vv) {
							$arr_reason = explode("-", $vv);
							if ($_COOKIE['userid'] == $arr_reason[0]) {
								$list["list"][$nkey]['cancel_flag'] = $arr_reason[1];
								$str_con = $arr_reason[0] . "-" . $arr_reason[1] . "-";
								$list["list"][$nkey]['cancel_reason']  = str_replace($str_con, "", $vv);
								break;
							}
						}
						$list["list"][$nkey]["return_flag"] = 1;
					}else{
						$list["list"][$nkey]["return_flag"] = 0;
					}
					$list["list"][$nkey]["external_buy"] = 0;
					$nkey = $nkey+1;
				}

				//新品开发

				if(count($list["list"]))
					$newkey = count($list["list"])+1;
				else
					$newkey=0;
				$where= " AND receivers like '%,".$userid.",%' ";
				$sku = trim($this->input->get('other'));
				if($sku){
					$where.= " AND sku = '{$sku}' ";
					$this->tpl->assign("other",$sku);
				}
				$status = trim($this->input->get('status'));
				if($status){
					if($status==1)
					{
						$where.=" AND return_receivers like '%,".$userid.",%'";
						//$where.=" AND return_receivers REGEXP({$userid})";
					}else{
						$where.=" AND (return_receivers IS null OR return_receivers NOT like '%,".$userid.",%')";
						//$where.=" AND (return_receivers IS null OR return_receivers NOT REGEXP ({$userid}))";
					}
				}
				$orderby = " ORDER BY return_reason asc ";
				$dates = $this->fun->getalllist('db_product_develop', $where, $nowpage, $if_csv, $this->pageSize, $orderby);
				$total = $dates["total"]+$total;
				$t_page = ceil($dates["total"] / $this->pageSize);
				if($t_page >$totalpage){
					$totalpage = $t_page;
				}

				foreach ($dates["list"] as $key => $value) {

					$list["list"][$newkey]["p_parent_name"] = $value['name'];
					$list["list"][$newkey]["sku"] = $value['sku'];
					$list["list"][$newkey]["id"] = $value['id'];
					$list["list"][$newkey]["protector"] = $value['createName'];
					$list["list"][$newkey]["linkUrl"] = $value['linkUrl'];

					$return_receiver = explode(",",$value['return_receivers']);
					$return_reason   = explode(",",$value['return_reason']);
					if(in_array($_COOKIE['userid'],$return_receiver)){
						$list["list"][$newkey]['cancel_flag'] = 0;
						$list["list"][$newkey]['cancel_reason'] = 0;
						foreach ($return_reason as $kk => $vv) {
							$arr_reason = explode("-", $vv);
							if ($_COOKIE['userid'] == $arr_reason[0]) {
								$list["list"][$newkey]['cancel_flag'] = $arr_reason[1];
								$str_con = $arr_reason[0] . "-" . $arr_reason[1] . "-";
								$list["list"][$newkey]['cancel_reason']  = str_replace($str_con, "", $vv);
								break;
							}
						}
						$list["list"][$newkey]["return_flag"] = 1;
					}else{
						$list["list"][$newkey]["return_flag"] = 0;
					}
					$list["list"][$newkey]["external_buy"] = 2;
					$newkey = $newkey+1;
				}
			}

			$pageList = $this->pagenav->page($total, $nowpage, $this->pageSize);
			$this->tpl->assign("totalpage", $totalpage); //计算最多页面的为总页数
			$this->tpl->assign("total", $total);         //当页所有记录数之和
			//$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
			//$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
			//$this->tpl->assign("total", $list["total"]);
			$this->tpl->assign("pageList", $pageList);
			$this->tpl->assign("recommendlist", $list["list"]);
			$this->tpl->display("welcome_recommendlist.tpl");
		}
		//显示更多最新入库
		if($op == 'showMoreStock')
		{
			$nowpage = $_REQUEST["pageid"];
			if ($nowpage == "")
				$nowpage = 1;
			$where = "";
			$sku = trim($this->input->get('other'));
			if($sku){
				$where.= " AND goods_sku = '{$sku}' ";
				$this->tpl->assign("other",$sku);
			}
			$where .= " AND istype =1 AND tostock =1 ";
			$orderby = " ORDER BY id DESC ";
			$list = $this->fun->getalllist('db_goods_handstorage', $where, $nowpage, $if_csv, $this->pageSize, $orderby);
			foreach ($list["list"] as $key => $value) {
				$storage = $this->db->get_where('db_goods_stock', " goods_id = '" . $rs["goods_id"] . "' and storage_id=1")->result_array();
				$list["list"][$key]["stock"] = $storage[0]['stock'];
				$list["list"][$key]["g_updatetime"] = date("Y-m-d",$value['intime']);
			}
			$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
			$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
			$this->tpl->assign("total", $list["total"]);
			$this->tpl->assign("pageList", $pageList);
			$this->tpl->assign("stocklist", $list["list"]);
			$this->tpl->display("welcome_stocklist.tpl");
		}
		if($op =="showMoreProduct"){
			$nowpage = $_REQUEST["pageid"];
			if ($nowpage == "")
				$nowpage = 1;
			$where = "";
			if($_COOKIE["classid"] ==12)	//工厂1人员
			{
				$g_supplier = 1;
			}
			if($_COOKIE["classid"] ==13)	//工厂2人员
			{
				$g_supplier = 2;
			}
			if($_COOKIE["classid"] ==16)	//外部开发
			{
				$g_supplier = 7;
			}

			$sku = trim($this->input->get('other'));
			if($sku){
				$where.= " AND goods_sku = '{$sku}' ";
				$this->tpl->assign("other",$sku);
			}
			if($g_supplier==1 || $g_supplier==2){
				$where.= " AND  g_supplier = {$g_supplier} AND  length(goods_parent_sku)=8 AND (locate(11,goods_parent_sku)=1 or locate(12,goods_parent_sku)=1 ) and length(goods_sku)=8 and  goods_sku NOT REGEXP '-' ";
			}elseif($g_supplier==7){
				$where.= " AND g_supplier = {$g_supplier} AND  goods_sku NOT REGEXP '-' ";
			}else{
				$where.= " AND length(goods_parent_sku)=8 AND (locate(11,goods_parent_sku)=1 or locate(12,goods_parent_sku)=1 ) and length(goods_sku)=8 and  goods_sku NOT REGEXP '-' ";
			}

			//$where .= " AND length(goods_parent_sku)=8 AND (locate(11,goods_parent_sku)=1 or locate(12,goods_parent_sku)=1 ) and length(goods_sku)=8 and  goods_sku NOT REGEXP '-' ";
			$orderby = " ORDER BY goods_id DESC ";
			$list = $this->fun->getalllist('db_goods', $where, $nowpage, $if_csv, $this->pageSize, $orderby);
			foreach ($list["list"] as $key => $value) {

				//获取推荐人员
				$arr_recommend = $this->db->get_where('db_product_develop', "sku='" . $value["goods_parent_sku"]."'")->row_array();
				if(count($arr_recommend)>0){
					$arr_receivers = explode(",",trim($arr_recommend['receivers'],","));
					$arr_return_receivers = explode(",",$arr_recommend['return_receivers']);
					$arr_cancel = array();
					if(count($arr_receivers)>0){
						foreach($arr_return_receivers as $k=>$v){
							if(in_array($v,$arr_receivers)){
								$cur_key = array_search($v,$arr_receivers);
								unset($arr_receivers[$cur_key]);
							}
							//查看当前是否存在
							$sql = "SELECT * FROM db_product_develop WHERE sku = '{$value['goods_parent_sku']}' AND  return_reason like '%{$v}-2%' ";
							$rr  = $this->db->query($sql)->row_array();
							if(count($rr)>0){
								$arr_cancel[] = $arr_return_receivers[$k]; //已经取消
								unset($arr_return_receivers[$k]);
							}
						}
					}
					$str_cancel = implode(",",$arr_cancel);
					$str_return_receivers = implode(",",$arr_return_receivers);
					//$str_return_receivers = $arr_recommend['return_receivers'];
					$str_receivers = implode(",",$arr_receivers);
					$rs_receivers['name'] = "";
					$rs_return_receivers['name'] = "";
					$rs_cancel['name'] = "";
					if($str_receivers){
						$sql_receivers = "SELECT GROUP_CONCAT(name) as name FROM d_user WHERE type = 1 AND  id in ({$str_receivers}) ";
						$rs_receivers  = $this->db->query($sql_receivers)->row_array();
					}
					if($str_return_receivers){
						$sql_return_receivers = "SELECT GROUP_CONCAT(name) as name FROM d_user WHERE type = 1 AND  id in ({$str_return_receivers}) ";
						$rs_return_receivers  = $this->db->query($sql_return_receivers)->row_array();
					}
					if($arr_cancel){
						$sql_cancel = "SELECT GROUP_CONCAT(name) as name FROM d_user WHERE type = 1 AND  id in ({$str_cancel}) ";
						$rs_cancel  = $this->db->query($sql_cancel)->row_array();
						foreach($rs_cancel as $k_c=>$v_c){
							$rs_cancel[$k_c] = "<s>".$v_c."</s>";
						}
					}

					$list["list"][$key]["str_cancel"] = $rs_cancel['name']?$rs_cancel['name']:"";
					$list["list"][$key]["str_receivers"] = $rs_receivers['name']?$rs_receivers['name']:"";
					$list["list"][$key]["str_return_receivers"] = $rs_return_receivers['name']?$rs_return_receivers['name']:"";
				}else{
					$list["list"][$key]["str_cancel"] = "";
					$list["list"][$key]["str_receivers"] = "";
					$list["list"][$key]["str_return_receivers"] = "";
				}

				//直发销量
				$this->load->model('Product_m', 'product');
				$arr_salenum = $this->product->getallstock($value["goods_parentID"]);

				$sql_goods = "SELECT GROUP_CONCAT(goods_id) as goods_id FROM db_goods WHERE goods_parent_sku = '{$value['goods_parent_sku']}' ";
				$rs_goods  = $this->db->query($sql_goods)->row_array();
				//总待发货
				$sql = "SELECT sum(goods_number) as goods_number FROM db_order_sell A left join db_order_list B on A.`order_id` = B.order_id where A.`goods_id` in ({$rs_goods['goods_id']}) AND B.order_status in (2,3) AND  B.is_nodelivery = 0  and B.shipping_status = 5 ";
				$ress = $this->db->query($sql)->row_array();
				$goods_number= 0;
				if($ress){
					$goods_number = $ress['goods_number'];
				}
				//销量
				$list["list"][$key]["saleNum"] = $arr_salenum['salenum']+$goods_number;

				$goods_images = $this->db->get_where('db_goods_images', 'goods_id=' . $value["goods_id"])->result_array();
				$thumb_url = $goods_images[0]["thumb_url"];
				if($thumb_url=="")
				{
					$list["list"][$key]["imagesurl"] = "/fuman/images/s-l500.jpg";
				}else{
					$list["list"][$key]["imagesurl"] = $goods_images[0]['thumb_url'];
				}
				$list["list"][$key]["g_updatetime"] = date("Y-m-d",strtotime($value['g_updatetime']));
			}
			$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
			$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
			$this->tpl->assign("total", $list["total"]);
			$this->tpl->assign("pageList", $pageList);
			$this->tpl->assign("productlist", $list["list"]);
			$this->tpl->display("welcome_productlist.tpl");
		}
		//显示更多停产商品、下架商品、清仓商品、重新销售商品信息
		if($op =="showMoreGoods"){
			$nowpage = $_REQUEST["pageid"];
			if ($nowpage == "")
				$nowpage = 1;
			$where = "";
			$sku = trim($this->input->get('other'));

			//$types 4 停产 7 下架 5 清仓 2重新销售
			$types = trim($this->input->get('types'));
			$this->tpl->assign("types",$types);
			if($sku){
				$where.= " AND number_sn = '{$sku}' ";
				$this->tpl->assign("other",$sku);
			}
			$supplier = $this->input->get('supplier');
			$this->tpl->assign("supplier",$supplier);
			if($supplier){
				$sele= " A LEFT JOIN db_goods B on A.number_id = B.goods_id  ";
				$where.=" AND  B.g_supplier ='".$supplier."' ";
				$this->tpl->assign("other",$sku);
			}
			$times = trim($this->input->get('times'));
			if($times){

				$tis = strtotime($times." 00:00:00");
				$tisto = strtotime($times." 23:00:00");
				$where.= " AND istips >='".$tis."' and istips <='".$tisto."' ";
				$this->tpl->assign("times",$times);
			}
			$where .= " AND types = '{$types}' ";
			$orderby = " ORDER BY id DESC ";
			$list = $this->fun->getalllist("db_sys_workbench $sele", $where, $nowpage, $if_csv, $this->pageSize, $orderby);
			foreach ($list["list"] as $key => $value) {
				$goods_images = $this->db->get_where('db_goods_images', 'goods_id=' . $value["number_id"])->result_array();
				
				$thumb_url = $goods_images[0]["thumb_url"];
				$list["list"][$key]['thumb_url'] = $thumb_url?$thumb_url:"/".$goods_images[0]["local_url"];
				if($value['istips']>0){
					$list["list"][$key]['instime'] = date("Y-m-d",$value['istips']);
				}else{
					$list["list"][$key]['instime'] = "";
				}
			}
			$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
			$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
			$this->tpl->assign("total", $list["total"]);
			$this->tpl->assign("pageList", $pageList);
			$this->tpl->assign("goodslist", $list["list"]);
			$this->tpl->assign("factory", $this->db->get('db_factory')->result_array());
			$this->tpl->display("welcome_goodslist.tpl");
		}
		//显示更多消息
		if($op =="showMoreMessage"){
			$nowpage = $_REQUEST["pageid"];
			if ($nowpage == "")
				$nowpage = 1;
			$where = "";
			/**
			$sku = trim($this->input->get('other'));
			if($sku){
				$where.= " AND number_sn = '{$sku}' ";
				$this->tpl->assign("other",$sku);
			}**/
			$userid = $_COOKIE['userid'];
			$where .= " AND message_toID = '{$userid}' ";
			$orderby = " ORDER BY id DESC ";
			$list = $this->fun->getalllist('db_message', $where, $nowpage, $if_csv, $this->pageSize, $orderby);
			foreach ($list["list"] as $key => $value) {
				$list["list"][$key]['instime'] = date("Y-m-d",$value['instime']);
			}
			$pageList = $this->pagenav->page($list["total"], $nowpage, $this->pageSize);
			$this->tpl->assign("totalpage", ceil($list["total"] / $this->pageSize));
			$this->tpl->assign("total", $list["total"]);
			$this->tpl->assign("pageList", $pageList);
			$this->tpl->assign("msglist",$list["list"]);
			$this->tpl->display("welcome_messagelist.tpl");
		}
		//删除更多消息
		if($op=="delMessage"){
			$str_ids = trim($this->input->post("str_ids"),",");
			$this->db->delete("db_message"," id in({$str_ids})");
			echo 200;exit;
		}


	}
}
